#!/bin/sh
#
# $FreeBSD$
#

# PROVIDE: ix-postinit
# REQUIRES: cron swaplate

. /etc/rc.subr

do_postinit()
{
	local IFS="|"
	local f="ini_type ini_command ini_script ini_when"
	eval local $f
	local sf=$(var_to_sf $f)

	# Sentinel file to tell we have gone far enough in the boot process.
	# See #17508
	touch /tmp/.bootready

	# Send event to middlewared saying we are late enough in the process
	# to call it ready
	/usr/local/bin/midclt call core.event_send system ADDED '{"id": "ready"}' > /dev/null

	${FREENAS_SQLITE_CMD} ${FREENAS_CONFIG} \
	"SELECT $sf FROM tasks_initshutdown WHERE ini_when = 'postinit' AND ini_enabled = 1 ORDER BY id" | \
	while eval read -r $f; do
		if [ "${ini_type}" = "command" ]; then
			eval ${ini_command}
		else
			if [ -e "${ini_script}" ]; then
				sh -c "exec ${ini_script}"
			fi
		fi
	done
}

name="ix-postinit"
start_cmd='do_postinit'
stop_cmd=':'

load_rc_config $name
run_rc_command "$1"
